﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UPnP
{
    public class SSDP
    {
        public SSDP()
        {
            Defaults();
        }

        private void Defaults()
        {
            TTL = 2;
            UserAgent = Environment.OSVersion.Platform.ToString() + "/" + Environment.OSVersion.Version.ToString() +" UPnP/1.1 UPnP4NET/1.0";
            MaxResponseTime = 1;
            Host = "239.255.255.250:1900";
            Target = SearchTargets.RootDevice;
            TargetStr = "";

        }

        public string GetRequest()
        {
            return  "M-SEARCH * HTTP/1.1\r\n" +
            "HOST: " + Host + "\r\n" +
            "ST:" + GetTarget() +  "\r\n" +     // upnp:rootdevice
            "MAN:\"ssdp:discover\"\r\n" +
            "MX:" + MaxResponseTime + "\r\n\r\n";
        }

        public string UserAgent { get; set; }
        public int TTL { get; set; }
        public int MaxResponseTime { get; set; } // MX
        public string Host { get; set; }

        public SearchTargets Target { get; set; }
        public string TargetStr { get; set; }

        public enum SearchTargets
        {
            RootDevice, // upnp:rootdevice
            All,        // ssdp:all
            UUID,       // uuid:<device-uuid>
        }

        private string GetTarget()
        {
            switch (Target)
            {
                case SearchTargets.All:
                    return "ssdp:all";
                case SearchTargets.RootDevice:
                    return "upnp:rootdevice";
                case SearchTargets.UUID:
                    return "uuid: " + TargetStr;
            }
            throw new Exception("Target is invalid");
        }
    }
}
